package com.zmc.dynamic.shard.strategy;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Properties;

/**
 * 单分片键策略
 * 注意：crud只有带分片键才会走分片键策略，根据分片键编写规则路由到指定表中
 */
public class StandardKeyGeneratorStrategy implements StandardShardingAlgorithm<String> {

    public static final String SHARD_STANDARD_STRATEGY = "standard_strategy";

    private Properties props = new Properties();

    /**
     * collection: 当前分片键所有表list
     * preciseShardingValue： 分片键（k-v），此处是单分片键，只会出现一对
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        //分片键逻辑，精准查询。可根据项目需求编写
        return null;
    }

    /**
     * collection: 当前分片键所有表list
     * rangeShardingValue： 分片键（k-v），
     * 注：k（分片键），v(范围，举例：0-2000;sql中between查询,且是分片键范围查询，则会触发)，其中v格式需要转义
     */
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {
        //分片键逻辑，范围查询。可根据项目需求编写
        return null;
    }

    @Override
    public Properties getProps() {
        return props;
    }

    @Override
    public void init(Properties properties) {
        this.props = properties;
    }

    @Override
    public String getType() {
        return SHARD_STANDARD_STRATEGY;
    }
}
